import {Component, HostBinding, OnInit} from '@angular/core';
import {Router, NavigationEnd} from '@angular/router';
import {ThemesService, SettingsService, TitleService} from '@delon/theme';
import {filter} from 'rxjs/operators';

@Component({
    selector: 'app-root',
    template: `<router-outlet></router-outlet>`
})
export class AppComponent implements OnInit {

    @HostBinding('class.layout-fixed') get isFixed() {
        return this.settings.layout.fixed;
    }

    @HostBinding('class.layout-boxed') get isBoxed() {
        return this.settings.layout.boxed;
    }

    @HostBinding('class.aside-collapsed') get isCollapsed() {
        return this.settings.layout.collapsed;
    }

    constructor(private theme: ThemesService,
                private settings: SettingsService,
                private router: Router,
                private titleSrv: TitleService) {
    }

    ngOnInit() {

        // if (this.isEmpty(this.settings.user)) {
        //     this.router.navigateByUrl('/passport/login')
        // }

        this.router.events
            .pipe(filter(evt => evt instanceof NavigationEnd))
            .subscribe(() => this.titleSrv.setTitle());
    }


    /*
     * 检测对象是否是空对象(不包含任何可读属性)。
     * 方法既检测对象本身的属性，也检测从原型继承的属性(因此没有使hasOwnProperty)。
     */
    isEmpty(obj) {
        for (var name in obj) {
            return false;
        }
        return true;
    };
}
